package core
{
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.events.KeyboardEvent;
	import flash.system.Security;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	
	import org.ais.event.TEvent;
	import org.ais.system.Ais;

	public class Main extends Sprite
	{
		protected var _volume:Number;
		
		public function Main()
		{
			try {
				Security.allowDomain("*");
				Security.allowInsecureDomain("*");
			}
			catch (error:*) {}
			__addEvent();
		}
		
		protected function __addEvent():void
		{
			Ais.IMain.stage.addEventListener(KeyboardEvent.KEY_DOWN, __stageKeyHandler);
			TEvent.newTrigger("IPLAYER_0", __triggerHandler);
		}
		
		protected function __removeEvent():void
		{
			Ais.IMain.stage.removeEventListener(KeyboardEvent.KEY_DOWN, __stageKeyHandler);
			TEvent.removeTrigger("IPLAYER_0", __triggerHandler);
		}
		
		protected function __stageKeyHandler(e:KeyboardEvent):void
		{
			if (e.target is TextField) return;
			switch (e.keyCode) {
				case Keyboard.SPACE:
					TEvent.trigger("IPLAYER_0", "TOGGLE_PAUSE");
					break;
				case Keyboard.UP:
					if (e.shiftKey === true) TEvent.trigger("IPLAYER_0", "MUTE");
					else if (e.ctrlKey === true) TEvent.trigger("IPLAYER_0", "SET_VOLUME", 1);
					else TEvent.trigger("IPLAYER_0", "SET_VOLUME", _volume + 0.1);
					break;
				case Keyboard.DOWN:
					if (e.ctrlKey === true) TEvent.trigger("IPLAYER_0", "SET_VOLUME", 0);
					else TEvent.trigger("IPLAYER_0", "SET_VOLUME", _volume - 0.1);
					break;
				case Keyboard.LEFT:
					var unsy:* = Ais.IMain.Swf.get("UNETSTREAMY_IMOON", "swf").content.getNetStream();
					if (e.shiftKey === true && Ais.IMain.Swf.hasKey("iplayer_1") !== -1) {
						var sh:* = Ais.IMain.Swf.getClass("iplayer_1", "core.model.PSharedObject").getInstance();
						var _p:uint = sh.data.p_int;
						if (e.ctrlKey === true) _p = 0;
						else _p === 0 ? (_p = sh.data.p_list.length - 1) : _p--;
						sh.data.p_int = _p;
						sh.flush();
						if (sh.data.p_list[_p]) TEvent.trigger("IPLAYER_0", "PLAY", sh.data.p_list[_p][0]);
						TEvent.trigger("PLAY_LISTOYITEM", "PLAY", _p);
						sh = null;
					}
					else if (e.ctrlKey === true) {
						if (unsy.isNormal === true) unsy.seekByPoint(0, 0);
						else unsy.seekByPoint(0);
					}
					else {
						var p:Number = unsy.getPlayProgress() - 0.05;
						p = p < 0 ? 0 : p;
						if (unsy.isNormal === true) unsy.seekByPoint(p * unsy.getMetaData().duration, 0);
						else unsy.seekByPoint(p);
					}
					unsy = null;
					break;
				case Keyboard.RIGHT:
					unsy = Ais.IMain.Swf.get("UNETSTREAMY_IMOON", "swf").content.getNetStream();
					if (e.shiftKey === true && Ais.IMain.Swf.hasKey("iplayer_1") !== -1) {
						sh = Ais.IMain.Swf.getClass("iplayer_1", "core.model.PSharedObject").getInstance();
						_p = sh.data.p_int;
						if (e.ctrlKey === true) _p = sh.data.p_list.length - 1;
						else _p === (sh.data.p_list.length - 1) ? (_p = 0) : _p++;
						sh.data.p_int = _p;
						sh.flush();
						if (sh.data.p_list[_p]) TEvent.trigger("IPLAYER_0", "PLAY", sh.data.p_list[_p][0]);
						TEvent.trigger("PLAY_LISTOYITEM", "PLAY", _p);
						sh = null;
					}
					else if (e.ctrlKey === true) {
						if (unsy.isNormal === true) unsy.seekByPoint(unsy.getMetaData().duration, 0);
						else unsy.seekByPoint(1);
					}
					else {
						if (unsy.isNormal === true) unsy.seekByPoint((unsy.getPlayProgress() + 0.05) * unsy.getMetaData().duration, 0);
						else unsy.seekByPoint(unsy.getPlayProgress() + 0.05);
					}
					unsy = null;
					break;
				case Keyboard.F:
					Ais.IMain.stage.displayState = Ais.IMain.stage.displayState === StageDisplayState.NORMAL ? StageDisplayState["FULL_SCREEN_INTERACTIVE"] : StageDisplayState.NORMAL;
					break;
			}
			e = null;
		}
		
		protected function __triggerHandler(type:String, data:* = null):void
		{
			switch (type) {
				case "GET_VOLUME":
					_volume = data;
					break;
			}
			type = null;
			data = null;
		}
		
		public function clear():void
		{
			__removeEvent();
		}
		
	}
}